<template>
  <div class="login">
    <div class="login_box">
      <div class="avatar_box">
        <img src="../assets/1.jpg" alt="" />
      </div>
      <el-form
        :rules="rules"
        :model="form"
        label-width="80px"
        ref="loginForm"
        style="margin-top: -45px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            prefix-icon="iconfont icon-user"
            v-model="form.username"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" v-model="form.password" prop="password">
          <el-input
            prefix-icon="iconfont icon-3702mima"
            v-model="form.password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="margin-left: 60px" @click="toLogin"
            >登录</el-button
          >
          <el-button
            type="warning"
            style="margin-left: 60px"
            @click="resetLoginForm"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      form: {
        username: "admin",
        password: "123456",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "用户名长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 6,
            max: 15,
            message: "密码长度在 6 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginForm.resetFields();
    },
    toLogin() {
      this.$refs.loginForm.validate(async (valid) => {
        if (valid) {
          let res = await this.$http.reqLogin(this.form);
          if (res.meta.status === 200) {
            this.$message({
              message: "登录成功",
              type: "success",
            });
            //保存token
            window.sessionStorage.setItem("token",res.data.token)
            //跳转页面
            this.$router.push('/home')
            return;
          }
          this.$message({
            message: "登录失败",
            type: "error",
          });
        } else {
          return;
        }
      });
    },
  },
};
</script>

 <style lang="less" scoped>
.login {
  background-image: url(../assets/bj.jpg);
  height: 100%;
}
.login_box {
  width: 450px;
  height: 300px;
  padding: 10px;
  padding-right: 30px;
  background-color: rgb(36, 28, 28);
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  .avatar_box {
    height: 130px;
    width: 130px;
    margin: 0 auto;
    border: 1px solid #eee;
    border-radius: 50%;
    box-shadow: 0 0 10px #ddd;
    transform:translateX(10%) translateY(-70%);
    user-select: none;
    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
}
</style>
